<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="[
        { name: 'box-base', title: '基础信息' },
        { name: 'box-spec', title: '规格及单位' },
         { name: 'box-zhuce', title: '注册证信息' },
         { name: 'box-his', title: '医保信息' },
         { name: 'box-sync', title: '同步情况' },
         { name: 'box-hos', title: '医院属性' },
        ]" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="cpPropRules" ref="editForm" :disabled="cpPropReadonly">
        <template #content>
          <div id="box-hos" class="box-title">医院属性</div>
          <customFormItem label="中标号" name="bidno" :parent="_self" :form="form"  />
          <customFormItem label="UDI" name="udi" :parent="_self" :form="form" width2x/>
          <customFormItem
            label="医院品类"
            name="hospitalCatalogName"
            :parent="_self"
            :form="form"
            disabled
          />

          <div id="box-base" class="box-title">基础信息
            <span class="span-tip" >仅部分属性可以修改，其余由院内推送更新</span>
            <span class="span-msg" >{{cpEnabledEdit.remark}}</span>
          </div>
          <customFormItem label="商品名称" name="materialName" :parent="_self" :form="form" disabled width2x/>
          <customFormItem label="商品类型" name="materialTypeCode"  disabled :parent="_self" :form="form"
                          :type="constCommon.formItemType.selectDict.name" :config="{dictType: constCommon.dictType.baseMatrType}"/>
          <customFormItem
            label="平台品类"
            name="catalogName"
            :parent="_self"
            :form="form"
            disabled
          />

          <customFormItem
            label="供应商"
            name="supplierName"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="$refs.tableDrawerSp.show()"
            :disabled="spDisabled"
            help="保存完不可修改"
            v-if="!utils.isSp()"
          />

          <customFormItem
            label="生产厂商" name="manufacturerName" :parent="_self" :form="form"
            @focusTo="$refs.manufacturerName.focus()"
            width2x
            disabled
          >
            <template #content>
              <mfgSelect
                v-model="form.manufacturerId"
                :valueDisplay="form.manufacturerName"
                @change="tableDrawerMfgConfirm"
                ref="manufacturerName"
              />
              <customButton
                title="+"
                help="列表不存在厂商时，可以新增厂商"
                theme="primary"
                size="small"
                @click="utilSelect.openSelectCreateFormMfg(tableDrawerMfgConfirm)"
                v-show="!cpPropReadonly"
                style="font-size: 20px; width: 20px;position: absolute;right: -22px;"
              >
              </customButton>
            </template>
          </customFormItem>

          <div id="box-spec" class="box-title">规格及单位</div>
          <customFormItem label="规格" name="materialSpec" :parent="_self" :form="form"  disabled/>
          <customFormItem label="寄售" name="isConsignment" :parent="_self" :form="form"  disabled
                          :type="constCommon.formItemType.switch.name"
          />
          <customFormItem label="标签管理" name="isOneThingOneYard" :parent="_self" :form="form"  disabled
                          :type="constCommon.formItemType.switch.name"
          />
          <customFormItem label="单价" name="price" :parent="_self" :form="form"  disabled/>
          <customFormItem label="单品单位" name="unit" :parent="_self" :form="form"  disabled help-bottom="最小单位"/>


          <customFormItem
            label="订单单位" name="orderUnit" :parent="_self" :form="form"
            @focusTo="$refs.orderUnit.focus()"
            help-bottom="采购订单使用的单位"
            disabled
          >
            <template #content>
              <unitSelect
                v-model="form.orderUnit"
                :valueDisplay="form.orderUnit"
                @change="orderUnitSelect"
                ref="orderUnit"
              />
            </template>
          </customFormItem>

          <customFormItem label="订单单位比率" name="orderQty" :parent="_self" :form="form"   :type="constCommon.formItemType.inputNumber.name"
                          help-bottom="即可以换算成多少个基本单位数量"
                          disabled
          />

          <div id="box-zhuce" class="box-title">注册证信息</div>
          <customFormItem label="注册证号" name="certificateNo" :parent="_self" :form="form" width2x disabled/>
          <customFormItem label="注册证开始" name="certificateExpiredStartDate" :parent="_self" :form="form"
                          disabled
                          :type="constCommon.formItemType.datePicker.name" />
          <customFormItem label="注册证截止" name="certificateExpiredDate" :parent="_self" :form="form"
                          disabled
                          :type="constCommon.formItemType.datePicker.name" />


          <div id="box-his" class="box-title">医保信息</div>
          <customFormItem label="医保代码27位" name="hisMedicareNo2" :parent="_self" :form="form" width2x disabled/>
          <customFormItem label="医保名称27位" name="hisMedicareName2" :parent="_self" :form="form" width2x disabled/>
          <customFormItem label="医保代码20位" name="hisMedicareNo" :parent="_self" :form="form" width2x disabled/>
          <customFormItem label="医保名称20位" name="hisMedicareName" :parent="_self" :form="form" width2x disabled/>


          <div id="box-sync" class="box-title">同步情况</div>
          <customFormItem label="院内最近修改" name="gmtModifiedLms" :parent="_self" :form="form" disabled/>
          <customFormItem label="外网最近修改" name="gmtModified" :parent="_self" :form="form" disabled/>
        </template>
      </customForm>
    </div>


    <!--供应商选择-->
    <tableDrawer ref="tableDrawerSp" refName="tableDrawerSp" @confirm="tableDrawerSpConfirm" viewKey="suppliers_crud" attach="body" loadForce/>

    <!--商品分类选择-->
    <goodsCategoryDrawer ref="goodsCategoryDrawer" :form="form"/>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from '@/components/custom/form/customUpload.vue'
import goodsCategoryDrawer from '@/components/custom/bus/goodsCategoryDrawer.vue'
import certificateSelect from '@/components/custom/bus/select/certificateSelect.vue'
import unitSelect from '@/components/custom/bus/select/unitSelect.vue'
import mfgSelect from '@/components/custom/bus/select/mfgSelect.vue'


export default {
  help: '医院管理-详情',
  data() {
    return {
      form: {},
      viewData: {},
      spDisabled: true,
    }
  },
  methods: {
    // 加载明细数据
    fetchChildData() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('materialId', this.form.materialId)
      } else {
        queryWrapper.ne(1, 1)
      }
      // this.$refs.childDataView.setQueryWrapper(queryWrapper)
    },
    // 当前登录供应商信息
    getCurrentSp() {
      let deptType = this.utils.getCurrentUser().deptType
      if (this.constCommon.deptType.supplier != deptType) {
        this.spDisabled = false
        return
      }
      let spInfo = this.utils.getSpInfo()
      if (spInfo && spInfo.id) {
        this.$set(this.form, 'supplierId', spInfo.id)
        this.$set(this.form, 'supplierName', spInfo.name)
        this.spDisabled = true
      }
    },

    // 厂商选择后
    tableDrawerMfgConfirm(row, refName) {
      this.$set(this.form, 'manufacturerId', row.id)
      this.$set(this.form, 'manufacturerName', row.name)
    },

    // 供应商选择后
    tableDrawerSpConfirm(row, refName) {
      this.$set(this.form, 'supplierId', row.id)
      this.$set(this.form, 'supplierName', row.name)
    },

    // 供应商选择后
    mfgFocus() {
      if (this.strings.isBlank(this.form.supplierId)) {
        this.utilMessage.warning('请先选择供应商')
        this.$refs.tableDrawerSp.show()
        return
      }
      this.$refs.tableDrawerMfg.show()
    },

    orderUnitSelect(row) {
      this.$set(this.form, 'orderUnit', row.name)
    },
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        let spType = this.form.spType || []
        if (spType.includes(this.constCommon.busConst.matrTypeMatr)){
          this.$set(this.form, 'isConsumable', 1)
        }
        if (spType.includes(this.constCommon.busConst.matrTypeDrug)){
          this.$set(this.form, 'isDrug', 1)
        }
        if (spType.includes(this.constCommon.busConst.matrTypeRreagent)){
          this.$set(this.form, 'isReagent', 1)
        }
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData) => {
        this.form = global.utils.jsonClone(formData)
        this.$set(this.form, 'id', this.form.id)
        if (this.propParent.cpFormNew) {
          this.$set(this.form, 'isDrug', 0)
          this.$set(this.form, 'isConsumable', 0)
          this.$set(this.form, 'isReagent', 0)
          this.getCurrentSp()
        } else {
          let spType = []
          if (this.form.isConsumable == 1) {
            spType.push(this.constCommon.busConst.matrTypeMatr)
          }
          if (this.form.isDrug == 1) {
            spType.push(this.constCommon.busConst.matrTypeDrug)
          }
          if (this.form.isReagent == 1) {
            spType.push(this.constCommon.busConst.matrTypeRreagent)
          }
          this.$set(this.form, 'spType', spType)
          if (!this.form.inactive || this.form.materialFlagDisabledScp) {
            setTimeout(() => {
              this.utilMessage.warning('该商品已禁用，不支持修改')
            }, 1000)
          }
        }
        this.fetchChildData()
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: { certificateSelect, customAnchor, customUpload, goodsCategoryDrawer,mfgSelect, unitSelect},
  computed: {
    cpPropRules() {
      let rule = this.propRules || {}
      rule.spType = [{ required: true, message: '至少选择一种供货类型', trigger: 'blur' }]
      return rule
    },
    // 是否只读
    cpEnabledEdit() {
      let enabled = false
      let remark = ''
      if (true == this.form.flagEnabledMaterialEdit) {
        enabled = true
        remark = '该医院已启用商品维护'
        if (this.strings.isNotBlank(this.form.enabledMaterialEditRemark)) {
          remark = this.form.enabledMaterialEditRemark
        }
      } else{
        remark = '该医院未启用商品维护'
      }
      return {
        enabled: enabled,
        remark: remark
      }
    },
    // 是否只读
    cpPropReadonly() {
      return this.propReadonly || !this.cpEnabledEdit.enabled
    }
  },
}
</script>
